import {
    isElementNode,
    isDirective
} from './utils';
import {
    regExgr
} from './regs';
import compilerUtils from './compiler-utils';
export default class Compiler {
    constructor (vm) {
       this.vm = vm;
       this.compile(this.vm.$el);
    }
    // 模版编译
    compile ($el) {
        const childrenNodes = $el.childNodes;
        [...childrenNodes].forEach( node => {
            if (isElementNode(node)) {
                this.compileElement(this.vm, node);
                this.compile(node);
            } else {
                this.compileText(this.vm, node)
            }
        })
    }
    // 判断元素节点
    compileElement (vm, node) {
        const attrs = node.attributes;
        [...attrs].forEach(attr => {
            const attrName = attr.name;
            if (isDirective(attrName)) {
                const attrValue = attr.value;
                const [, vType] = attrName.split('-');
                compilerUtils[vType](vm, attrValue, node);
            }
        })
    }
    // 文本节点
    compileText (vm, node) {
        console.log(node.textContent);
        const text = node.textContent; 
        if (regExgr.test(text)) {
             compilerUtils['text'](vm, text, node);
        }
    }
}